package week2;

public class LinkedList<T> implements List<T> {
    private static Object[] elements;
    private static int length;





    public LinkedList() {
        this.elements = new Object[20];
        this.length = 0;
    }


    public LinkedList(T[] data){
        this.elements = new Object[data.length];
        for(int i = 0; i < data.length; i++) {
            this.elements[i] = data[i];
        }

        this.length = data.length;
    }

    public static Object[] getElements() {
        return elements;
    }

    public static void setElements(Object[] elements) {
        LinkedList.elements = elements;
    }

    public static int getLength() {
        return length;
    }

    public static void setLength(int length) {
        LinkedList.length = length;
    }

    public static void main(String[] args) {
        Integer[] data = {1, 2, 3, 5, 78, 8, 9, 4, 0,66,36, 23, 18, 90, 88, 77, 7, 13, 27,909};
        LinkedList<Integer> linkedList = new LinkedList<Integer>(data);
        System.out.println("--打印线性表--");
        System.out.println(linkedList);
        System.out.println("查找第15个元素");
        System.out.println(linkedList.search(15));
        System.out.println("删除第6个元素");
        linkedList.remove(6);
        System.out.println(linkedList);
        System.out.println("在第19个元素，插入元素22");
        linkedList.insert(19,22);
        System.out.println(linkedList);
        System.out.println("计算链表的长度");
        System.out.println(linkedList.size());
        System.out.println("打印线性表中最大的数和最小的数");
        System.out.println(linkedList.getMax());
        System.out.println(linkedList.getMin());
        System.out.println("逆置线性表");
        linkedList.reverse( linkedList.getElements(),20);
        System.out.println(linkedList);
        System.out.println("合并2个有序链表");


    }
    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public String toString() {
        String s = "";
        for (int i = 0; i < this.length; i++) {
            s += this.elements[i] + " ";
        }

        if(s.length() == 0) {
            return s;
        }
       //System.out.println(length);     //检测长度
        return s.substring(0,s.length() - 1);
    }

    @Override
    public int size() {
        return this.length;
    }

    @Override
    public T get(int i) {
        return null;
    }

    @Override
    public void set(int i, T x) {

    }

    @Override
    public int insert(int i, T x) {
        if(i < 1 || i > this.length+1)      //判断输入的位置是否合法
            return -1;
        if(this.length == this.elements.length)         //判断顺序表是否已满
            return -2;


        for (int j = this.length-1; j >= i-1 ; j--) {//     后移
            this.elements[j+1] = this.elements[j];
        }
        this.elements[i-1] = x;
        this.length++;

        return 0;
    }

    @Override
    public int insert(T x) {
        return 0;
    }

    @Override
    public T remove(int i) {
        if(i < 1 || i > this.length) {
            return null;
        }else if(i == this.length) {
            this.length--;
            return null;
        }else {
            for (int j = i - 1; j < this.length-1; j++) {  //从要删除元素的位置开始，将后面的元素逐个向前移动一位，填补删除元素后的空缺
                this.elements[j] = this.elements[j + 1];
            }
            this.length--;
            return  null;
        }
    }

    @Override
    public void clear() {

    }

    @Override
    public int search(T key) {
        int i = (Integer) key - 1;
        return (int) this.elements[i];
    }

    @Override
    public T removeByKey(T key) {
        return null;
    }
    public String getMax(){     //最大值
        int t = Integer.MIN_VALUE;
        for (int i = 0; i < this.length; i++){
            if((int)this.elements[i] > t){
                t = (int) this.elements[i];
            }
        }
        return "最大值：" + t;
    }

    public String getMin(){      //最小值
        int t = Integer.MAX_VALUE;
        for (int i = 0; i < this.length; i++){
            if((int)this.elements[i] < t){
                t = (int) this.elements[i];
            }
        }
        return "最小值：" + t;
    }
    public  void reverse(Object[] a, int n){
        int i,j;
        int temp;
        for(i = 0,j = n-1; i < j; i++,j--){
            temp = (Integer) a[i];
            a[i] = a[j];
            a[j] = temp;
        }


        // return "逆置后的数据" + this.toString();
    }
}
